<?php


namespace app\common\utils;


class Helper
{
    /**
     * @param $num
     * @param $total
     * @return false|float|string
     * Created by chenyawei on 2024/7/16
     * Description:除发
     */
    public static function getDiv($num,$total){
        return ($num==0||$total==0)?"0.00":(round(($num/$total)*100,2));
    }

    /**
     * @param $num
     * @param $total
     * @return false|float|string
     * Created by chenyawei on 2024/7/16
     * Description:计算除 +%
     */
    public static function getRate($num,$total){
        return ($num==0||$total==0)?"0.00%":(round(($num/$total)*100,2).'%');
    }
    
    public static function getSum($array,$field=""){
        return array_sum(array_column($array,$field));
    }

    /**
     * @param $year
     * @param $isReverse
     * @return array
     * Created by chenyawei on 2024/12/11
     * Description:根据年份生成月份数据
     */
    public static function getMonthsByYear($year, $isReverse= true){
        $currentYear = date("Y"); 
        $currentMonth = date("m"); 
        $months = [];
        
        if ($year > $currentYear) {
            return $months;
        }
        if ($year == $currentYear) {
            for ($i = 1; $i <= $currentMonth; $i++) {
                $months[] = $year . '-' . str_pad($i, 2, '0', STR_PAD_LEFT); 
            }
        } else {
            for ($i = 1; $i <= 12; $i++) {
                $months[] = $year . '-' . str_pad($i, 2, '0', STR_PAD_LEFT); 
            }
        }
        if($isReverse){
            $months =   array_reverse($months);
        }
        return $months;
    }


    /**
     * @param $year
     * @param $month
     * @param $isReverse
     * @return array
     * Created by chenyawei on 2024/12/11
     * Description:获取某一月的天数
     */
    public static  function getDaysByMonth($year, $month, $isReverse = true ) {
        $currentYear = date("Y");
        $currentMonth = date("m");

        if ($year > $currentYear) {
            return [];
        }
        if ($month > $currentMonth) {
            return [];
        }
        $daysInMonth = date("t",strtotime("{$year}-{$month}"));
        $days = [];

        for ($day = 1; $day <= $daysInMonth; $day++) {
            $days[] = $year . '-' . str_pad($month, 2, '0', STR_PAD_LEFT) . '-' . str_pad($day, 2, '0', STR_PAD_LEFT);
        }
        if($isReverse){
            $days =   array_reverse($days);
        }
        return $days;
    }
}